<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
// THESE FUNCTIONS ARE FOR MYSQL DATABASES
class dbmanager {

	var $db;
	var $res;
	var $error;
	var $errno;
	
	var $_host;
	var $_user;
	var $_pass;

	public function __construct($host, $user, $pass) {
		$this->_host = $host;
		$this->_user = $user;
		$this->_pass = $pass;
	}

	public function testConnect($database) {
		$t = mysql_connect($this->_host, $this->_user, $this->_pass);
		if (!$t) {
			$this->error=mysql_error();
			$this->errno=mysql_errno();
			return false;
		}
		$t = mysql_select_db($database);
		if (!$t) {
			$this->error=mysql_error();
			$this->errno=mysql_errno();
			return false;
		}
		return true;
	}

	public function connect($database) {
		debug("connect");
		$db = mysql_connect($this->_host, $this->_user, $this->_pass) or die('Error al conectar a la base de datos. ' . mysql_error());
		mysql_select_db($database) or die('Error al seleccionar la base de datos.' . mysql_error());
		$this->db = $db;
		$this->error=mysql_error();
		$this->errno=mysql_errno();
	}

	public function query($sql) {
		debug("query");
		dblog($sql);
		$this->res = mysql_query($sql);
		$this->error=mysql_error();
		$this->errno=mysql_errno();		
	}
	
	public function execute($sql) {
		$res = mysql_unbuffered_query($sql);
		$this->res=$res;
		$this->error=mysql_error();
		$this->errno=mysql_errno();
		return $res;		
	} 

	public function num_rows() {
		debug("num_rows");
		return mysql_num_rows($this->res);
	}

	public function affected_rows() {
		$ret = mysql_affected_rows();
		dblog("affected_rows ".$ret);
		return $ret;
	}

	public function fetch_array() {
		debug("fetch_array");
		return mysql_fetch_array($this->res);
	}

	public function fetch_object() {
		debug("fetch_object");
		return mysql_fetch_object($this->res);
	}

	public function fetch_row() {
		debug("fetch_row");
		return mysql_fetch_row($this->res);
	}

	public function close() {
		debug("close");
		mysql_close($this->db);
	}

	public function exists($table) {
		return mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"));
	}
}
?>
